unit uGraphicFunctions;

interface

uses GlMaterial, Classes;
// Materials Add
Function AddSimpleMaterial(aMatLib: TGlMaterialLibrary; aMaterialName: String;
  aR, aG, aB, aAlpha: byte): TGlLibMaterial;
Function AddMaterial(aMatLib: TGlMaterialLibrary;
  aFileName, aMaterialName: String): TGlLibMaterial; overload;
Function AddMaterial(aMatLib: TGlMaterialLibrary; aStream: TStream;
  aMaterialName: String): TGlLibMaterial;  overload;
Procedure AddMaterials(aMatLib: TGlMaterialLibrary; aFolder: String;
  aFiles: array of String; aNames: array of String);

implementation

uses GlTexture, GlCompositeImage, GlFilePNG, GlFileJPeG;

Procedure AddMaterials(aMatLib: TGlMaterialLibrary; aFolder: String;
  aFiles: array of String; aNames: array of String);
var
  i: integer;
begin
  for i := low(aFiles) to high(aFiles) do
    if (i >= low(aNames)) and (i <= high(aNames)) then
      AddMaterial(aMatLib, aFolder + aFiles[i], aNames[i]);
end;

Function AddSimpleMaterial(aMatLib: TGlMaterialLibrary; aMaterialName: String;
  aR, aG, aB, aAlpha: byte): TGlLibMaterial;
begin
  result := aMatLib.Materials.Add;
  with result do
  begin
    with Material do
    begin
      MaterialOptions := [moIgnoreFog, moNoLighting];
      Texture.Disabled := true;
      with FrontProperties do
      begin
        Ambient.SetColor(aR / 255, aG / 255, aB / 255, aAlpha / 255);
        Diffuse.SetColor(aR / 255, aG / 255, aB / 255, aAlpha / 255);
        Emission.SetColor(aR / 255, aG / 255, aB / 255, aAlpha / 255);
        Specular.SetColor(aR / 255, aG / 255, aB / 255, aAlpha / 255);
      end;
    end;
    Name := aMaterialName;
  end;
end;

Function AddMaterial(aMatLib: TGlMaterialLibrary;
  aFileName, aMaterialName: String): TGlLibMaterial;
begin
  result := aMatLib.Materials.Add;
  with result do
  begin
    with Material do
    begin
      MaterialOptions := [moIgnoreFog, moNoLighting];
      Texture.Disabled := false;
      BlendingMode := bmTransparency;
      Texture.TextureMode := tmModulate;
      with FrontProperties do
      begin
        Ambient.SetColor(1, 1, 1, 1);
        Diffuse.SetColor(1, 1, 1, 1);
        Emission.SetColor(1, 1, 1, 1);
        Specular.SetColor(1, 1, 1, 1);
      end;
      Texture.ImageClassName := 'TGLCompositeImage';
      TGLCompositeImage(Texture.Image).LoadFromFile(aFileName);
      // Texture.AllocateHandle;
    end;
    Name := aMaterialName;
  end;
end;

Function AddMaterial(aMatLib: TGlMaterialLibrary; aStream: TStream;
  aMaterialName: String): TGlLibMaterial;
begin
  result := aMatLib.Materials.Add;
  with result do
  begin
    with Material do
    begin
      MaterialOptions := [moIgnoreFog, moNoLighting];
      Texture.Disabled := false;
      BlendingMode := bmTransparency;
      Texture.TextureMode := tmModulate;
      with FrontProperties do
      begin
        Ambient.SetColor(1, 1, 1, 1);
        Diffuse.SetColor(1, 1, 1, 1);
        Emission.SetColor(1, 1, 1, 1);
        Specular.SetColor(1, 1, 1, 1);
      end;
      Texture.ImageClassName := 'TGLCompositeImage';
      TGLCompositeImage(Texture.Image).LoadFromStream(aStream);
      // Texture.AllocateHandle;
    end;
    Name := aMaterialName;
  end;
end;

end.
